#******************************************************************
#
# Copyright 2017 Microsoft
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

import os

Import('ipca_env')
target_arch = ipca_env.get('TARGET_ARCH')
elevator_client_uwp_env = ipca_env.Clone()

# Convert architecture to the platform value used by Visual Studio.
platform_str = target_arch
if platform_str == 'amd64':
    platform_str = 'x64'
elif platform_str == 'x86':
    platform_str = 'Win32'

app_package_folder_name = 'ElevatorClientUWP_1.0.0.0_Test'
if not elevator_client_uwp_env.get('RELEASE'):
    platform_str += '_Debug'
    app_package_folder_name = 'ElevatorClientUWP_1.0.0.0_Debug_Test'

solutiondir = 'resource\\IPCA\\samples\\ElevatorClientUWP'
solutionfile = os.path.join(elevator_client_uwp_env.get('SRC_DIR'),
                            solutiondir,
                            'ElevatorClientUWP.sln')

# Construct output directory
build_path = 'resource\\IPCA\\samples\\ElevatorClientUWP'
build_outdir = os.path.normpath(elevator_client_uwp_env.get('BUILD_DIR') + build_path) + '\\'
outdir = build_outdir + '\\'

app_output_name = 'ElevatorClientUWP_1.0.0.0_' + platform_str
appx_file = build_outdir + 'ElevatorClientUWP\\' + app_output_name + '.appx'

# Set the MSBuildOutDir env variable so that the MSBuilder can use it
# Note: This should only be set on a cloned Environment and not the global one
elevator_client_uwp_env['MSBuildOutDir'] = outdir

if elevator_client_uwp_env.GetOption('clean'):
    elevator_client_uwp_env.MSBuildClean(appx_file, solutionfile)
else:
    elevator_client_uwp_build = elevator_client_uwp_env.MSBuild(appx_file, solutionfile)
    elevator_client_uwp_env.AlwaysBuild('elevator_client_uwp_build')

# Run the WACK test if we are building/running Release Tests
if elevator_client_uwp_env.get('TEST') == '1' \
   and elevator_client_uwp_env.get('RELEASE') \
   and not elevator_client_uwp_env.GetOption('clean'):
    cert_file =  build_outdir + 'ElevatorClientUWP\\AppPackages\\' + app_package_folder_name + \
                 '\\' + app_output_name + '.cer'

    from tools.scons.RunTest import *
    run_uwp_wack_test(elevator_client_uwp_env, cert_file, appx_file, build_outdir)
